﻿using System;
using System.Collections.Generic;
using System.Text;
using Lswweb.Model;
using Lswweb.Domain;
using Lswweb.IDAL;

namespace Lswweb.Domain
{
    public class News
    {
        private static INews newsDAL = DALFactory.CreateNewsDAL();

        #region 查询

        public static NewsInfo GetNews(int newsId)
        {
            if (newsId <= 0)
                return null;

            return newsDAL.GetNews(newsId);
        }

        public static NewsInfo GetFirstNews(int categoryId)
        {
            return newsDAL.GetFirstNews(categoryId,SiteContext.Current.Language);
        }

        public static IList<NewsInfo> GetNewsList(string searchKey, SearchKeyType searchKeyType, int categoryId, StatusType type, AuditingStatusType auditingStatus, BooleanType isCommand, BooleanType isTop, DateTime maxDateTime, DateTime minDateTime, int pageIndex, int pageSize, out int totalRecord)
        {
            return newsDAL.GetNewsList(searchKey, searchKeyType, categoryId, type,auditingStatus, isCommand, isTop, maxDateTime, minDateTime,SiteContext.Current.Language, pageIndex, pageSize, out totalRecord);
        }

        #endregion

        #region 更新

        public static void UpdateDisplayOrder(int newsId, int displayOrder)
        {
            if (newsId <= 0)
            {
                SiteContext.RenderMessage(ExceptionType.UnknownError);
                return;
            }

            string url = SiteContext.Current.ReferrerUrl;
            if (newsDAL.UpdateDisplayOrder(newsId, displayOrder))
                SiteContext.RenderMessage(ExceptionType.HandleSuccess,url);
            else
                SiteContext.RenderMessage(ExceptionType.UnknownError,url);
        }

        public static void Update(NewsInfo newsInfo)
        {
            newsInfo = NewsHandler(newsInfo);

            if (newsDAL.Update(newsInfo))
            {
                if (!string.IsNullOrEmpty(newsInfo.ImageUrl))
                    newsDAL.UpdateImageUrl(newsInfo.NewsId, newsInfo.ImageUrl);
                SiteContext.RenderMessage(ExceptionType.HandleSuccess);
            }
            else
            {
                SiteContext.RenderMessage(ExceptionType.UnknownError);
            }
        }

        public static void UpdateIsHot(int id, bool isHot)
        {
            if (id <= 0)
            {
                SiteContext.RenderMessage(ExceptionType.UnknownError);
                return;
            }

            string url = SiteContext.Current.Context.Request.UrlReferrer.PathAndQuery;
            if (newsDAL.UpdateIsHot(id, isHot))
                SiteContext.RenderMessage(ExceptionType.HandleSuccess, url);
            else
                SiteContext.RenderMessage(ExceptionType.UnknownError);
        }
        
        public static void UpdateIsTop(int id, bool isTop)
        {
            if (id <= 0)
            {
                SiteContext.RenderMessage(ExceptionType.UnknownError);
                return;
            }

            string url = SiteContext.Current.Context.Request.UrlReferrer.PathAndQuery;
            if (newsDAL.UpdateIsTop(id, isTop))
                SiteContext.RenderMessage(ExceptionType.HandleSuccess, url);
            else
                SiteContext.RenderMessage(ExceptionType.UnknownError);
        }

        #endregion

        #region 添加/删除

        public static void Insert(NewsInfo newsInfo)
        {
            newsInfo = NewsHandler(newsInfo);
            if (newsDAL.Insert(newsInfo) > 0)
                SiteContext.RenderMessage(ExceptionType.HandleSuccess);
            else
                SiteContext.RenderMessage(ExceptionType.UnknownError);
        }

        public static void Delete(int newsId)
        {
            string returnUrl = SiteContext.Current.ReferrerUrl;
            NewsInfo info = GetNews(newsId);
            if (info ==null)
            {
                SiteContext.RenderMessage(ExceptionType.UnknownError, returnUrl);
                return;
            }

            if (!string.IsNullOrEmpty(info.ImageUrl))
            {
                Globals.FileDelete(ImageHelper.GetThumbnailUrl(info.ImageUrl, false));

                Globals.FileDelete(info.ImageUrl);
            }


            ExceptionType type = ExceptionType.UnknownError;
            if (newsDAL.Delete(newsId))
                type = ExceptionType.HandleSuccess;
            SiteContext.RenderMessage(type, returnUrl);
        }

        #endregion

        public static string GetCategoryName()
        {
            return GetCategoryName(SiteContext.Current.CategoryId);
        }
        public static string GetCategoryName(int categoryId)
        {
            TreeTables treeTable = TreeTables.NewsCategories;
            return treeTable.GetCategoryName(categoryId);
        }



        public static NewsInfo GetNextNews(int categoryId, int newsId)
        {
            return newsDAL.GetNextNews(newsId, categoryId, SiteContext.Current.Language);
        }


        public static NewsInfo GetPrevNews(int categoryId, int newsId)
        {
            return newsDAL.GetPrevNews(newsId, categoryId, SiteContext.Current.Language);
        }

        private static NewsInfo NewsHandler(NewsInfo newsInfo)
        {
            if (newsInfo == null)
            {
                SiteContext.RenderMessage(ExceptionType.UnknownError);
                return null;
            }

            if (newsInfo.NewsId <= 0 && SiteContext.Current.Id > 0)
                newsInfo.NewsId = SiteContext.Current.Id;

            newsInfo.Content = ImageHelper.MoveEditorImagesToDirectory(SiteModule.News,newsInfo.Content);
            newsInfo.ImageUrl = ImageHelper.MoveImagesToDirectory(SiteModule.News, newsInfo.ImageUrl);
            //ImageHelper.MoveEditorImagesToDirectory(SiteModule.News,ImageHelper.GetThumbnailUrl(newsInfo.ImageUrl,true));

            if (string.IsNullOrEmpty(newsInfo.Ver))
                newsInfo.Ver = SiteContext.Current.Language;

            return newsInfo;
        }
    }

    public class NewsSkins
    {
        private static INewsSkins skinDAL = DALFactory.CreateNewsSkinDAL();

        public static string GetSkin(int newsCategoryId)
        {
            string skin=string.Empty;
            CategoryInfo info = TreeTables.NewsCategories.GetModelById(newsCategoryId);
            if (info == null)
                return skin;

            string[] list = info.Path.Split('/');
            for (int i = list.Length; i > 0;i-- )
            {
                int id = Convert.ToInt32(list[i-1]);
                NewsSkinInfo skinInfo = skinDAL.GetSkin(id);
                if (skinInfo != null)
                    return skinInfo.Skin;
            }

            return skin;
        }

        public static CategoryInfo GetSkinCategory(int newsCategoryId)
        {
            CategoryInfo info = TreeTables.NewsCategories.GetModelById(newsCategoryId);
            if (info == null)
                return null;

            string[] list = info.Path.Split('/');
            for (int i = list.Length; i > 0; i--)
            {
                int id = Convert.ToInt32(list[i-1]);
                NewsSkinInfo skinInfo = skinDAL.GetSkin(id);
                if (skinInfo != null)
                    return TreeTables.NewsCategories.GetModelById(id);
            }

            return null;
        }
        
    }

    public class NewsConst
    {
        /// <summary>
        /// 商城动态
        /// </summary>
        public const int NewsBaseNodeId = 3;
    }
}
